/*
 * vme.c
 *
 *  Created on: 2012. 5. 7.
 *      Author: root
 */

#include "../includes.h"

_VME VME;

//==============================//
// VME BlockRam Initialize		//
//==============================//
void Init_BlockRam() {
	VME_BlockRam_Write(BLOCKRAM_ADDR_INIT,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_YEAR,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_MONTH_DAY,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_HOUR_MIN,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_SECOND,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LAT_Deg_Min,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LAT_Sec_int,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LAT_Sec_Frac,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LON_Deg_Min,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LON_Sec_int,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LON_Sec_Frac,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_HEIGHT,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_HEADING,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_CV,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_PTP_Sec,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_PTP_nSec_1,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_PTP_nSec_2,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_Accuracy_Sec,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_Accuracy_nSec_1,0x0);
	VME_BlockRam_Write(BLOCKRAM_ADDR_Accuracy_nSec_2,0x0);
}

//================//
// VME Initialize //
//================//
void Init_VME() {
	int vme_tmp = 0;

	if (CONFIG.VMEMODE == 0)	return;

	Init_BlockRam();

	VME.Data.Init_Request 			= 0;
	VME.Data.Init_Result 			= 0;
	VME.Data.Bit_Request 			= 0;
	VME.Data.Visible 				= 0;
	VME.Data.Tracking 				= 0;
	VME.Data.TOD_Valid 				= 0;
	VME.Data.FPGA_Valid 			= 0;
	VME.Data.Temp_Valid 			= 0;
	VME.Data.Power_Valid 			= 0;
	VME.Data.GPS_Lock 				= 0;
	VME.Data.Year 					= 0;
	VME.Data.Month 					= 0;
	VME.Data.Day 					= 0;
	VME.Data.Hour 					= 0;
	VME.Data.Minute 				= 0;
	VME.Data.Second 				= 0;
	VME.Data.Lat_Degree 			= 0;
	VME.Data.Lat_sign 				= 0;
	VME.Data.Lat_Minute 			= 0;
	VME.Data.Lat_Second_int 		= 0;
	VME.Data.Lat_Second_frac 		= 0;
	VME.Data.Lon_Degree 			= 0;
	VME.Data.Lon_sign 				= 0;
	VME.Data.Lon_Minute 			= 0;
	VME.Data.Lon_Second_int 		= 0;
	VME.Data.Lon_Second_frac 		= 0;
	VME.Data.height_sign 			= 0;
	VME.Data.height 				= 0;
	VME.Data.Heading 				= 0;
	VME.Data.PTP_Data 				= 0;
	VME.Data.Accuracy_Sec 			= 0;
	VME.Data.Accuracy_nSec_1 		= 0;
	VME.Data.Accuracy_nSec_2 		= 0;
	VME.Data.Geodetic_Mode 			= 0;
	VME.Data.M_S_Mode 				= 0;
	VME.Data.TimeSync_Mode 			= 0;

	VME.BlockRam.Boart_ID 			= 0;
	VME.BlockRam.Bit 				= 0;
	VME.BlockRam.Year 				= 0;
	VME.BlockRam.Month_Day 			= 0;
	VME.BlockRam.Hour_min 			= 0;
	VME.BlockRam.Second 			= 0;
	VME.BlockRam.Lat_Deg_Min 		= 0;
	VME.BlockRam.Lat_Sec_Int 		= 0;
	VME.BlockRam.Lat_Sec_Frac 		= 0;
	VME.BlockRam.Lon_Deg_min 		= 0;
	VME.BlockRam.Lon_Sec_Int 		= 0;
	VME.BlockRam.Lon_Sec_Frac 		= 0;
	VME.BlockRam.height 			= 0;
	VME.BlockRam.Heading 			= 0;
	VME.BlockRam.CV 				= 0;
	VME.BlockRam.PTP_Data_Sec 		= 0;
	VME.BlockRam.PTP_Data_nSec_1 	= 0;
	VME.BlockRam.PTP_Data_nSec_2 	= 0;
	VME.BlockRam.Accuracy_Sec 		= 0;
	VME.BlockRam.Accuracy_nSec_1 	= 0;
	VME.BlockRam.Accuracy_nSec_2 	= 0;
	VME.BlockRam.VME_Sync_Mode 		= 0;

	CONFIG.VMEMODE 			= 1;
	CONFIG.NETSEND 			= 0;
	CONFIG.SYSTEM_DELAY 	= 0;
	CONFIG.WriteEvent 		= ON;

	VME.CV_Data.Tick 				= 0;
	VME.CV_Data.Rang_Count 			= 0;
	VME.CV_Data.Satxyz_Count 		= 0;
	VME.CV_Data.CV_Trans_Period 	= 1;
	VME.CV_Data.time				= 0;
	VME.CV_Data.Compare_Error		= 0;
	VME.CV_Data.CV_Value_Sum 		= 0;
	VME.CV_Data.CV_Value_Sum_Count	= 0;
	VME.CV_Data.CV_Data_Disable 	= OFF;

	VME.CV_Data.Buf[VME.CV_Data.Tick].full			= 0;
	VME.CV_Data.Buf[!(VME.CV_Data.Tick)].full		= 0;
	VME.CV_Data.Buf[VME.CV_Data.Tick].size			= 0;
	VME.CV_Data.Buf[!(VME.CV_Data.Tick)].size		= 0;

	if (VME_init_count == ON) {
		VME_BlockRam_Write(BLOCKRAM_ADDR_SYNC_MODE, 0x08);
		VME_init_count = OFF;
	}

}

//===============================//
// 1초마다 수행되는 VME 관련된 함수   //
//===============================//

int SyncCount_10Mhz_1PPS = 1;
void VME_Function()
{
	//VME_Interrupt_handling();
	VME_Update_Data();
	VME_Update_BlockRam_Data();
	VME_Write_BlockRam();

	//12-06-21  sewon 10Mhz<->1PPS Rising Edge Control(???????)

	if(SyncCount_10Mhz_1PPS==1){
		SyncCount_10Mhz_1PPS=0;
		if( detail_cnt>=15 )			SetFpgaValue( 19, 0x1E );
		else if( detail_cnt<15 )		SetFpgaValue( 19, 0x0A );
	}

	VME.CV_Data.Compare_Error++;//120626 sewon, CV Slave(Compare Error => GPS Control!)

	VME_Read_BlockRam();

	if (SYSTEM.EB.LOCKMODE == EB_LOCKED)	VME.CV_Data.time++;
	else									VME.CV_Data.time = 0;

	if(VME.CV_Data.time>30*60)		VME.CV_Data.CV_Trans_Period = 12;
	else							VME.CV_Data.CV_Trans_Period = 1;

}


void VME_Update_Data() {
	int fd = fdFPGA;

	if (CONFIG.VMEMODE == 0)		return;

//	VME.Data.Init_Value 		= 0;
//	VME.Data.Init_Result 		= 0;
//	VME.Data.Bit_Request 		= 0;
	VME.Data.Visible = SYSTEM.TRACK.VISIBLE;
	VME.Data.Tracking = SYSTEM.TRACK.TRACKED;

	if (TOD.status != 0)		VME.Data.TOD_Valid = 0;
	else if (TOD.status == 0)	VME.Data.TOD_Valid = 1;
	VME.Data.FPGA_Valid = GetFPGADone();
	VME.Data.Temp_Valid = DIAG.TEMPERATURE;
	VME.Data.Power_Valid = DIAG.POWER;

	VME.Data.GPS_Lock = ((SYSTEM.EB.LOCKMODE) ^ (ON));
	VME.Data.Year = SYSTEM.TIME.TOUT.YEAR;
	VME.Data.Month = SYSTEM.TIME.TOUT.MONTH;
	VME.Data.Day = SYSTEM.TIME.TOUT.DAY;
	VME.Data.Hour = SYSTEM.TIME.TOUT.HOUR;
	VME.Data.Minute = SYSTEM.TIME.TOUT.MINUTE;
	VME.Data.Second = SYSTEM.TIME.TOUT.SECOND;

	if (SYSTEM.POSITION.CURRENT.LAT > 0)		VME.Data.Lat_sign = 0;
	else if (SYSTEM.POSITION.CURRENT.LAT < 0)	VME.Data.Lat_sign = 1;
	//GPGGA<<---initialize//
//	VME.Lat_Degree 		= 0;
//	VME.Lat_Minute 		= 0;
//	VME.Lat_Second_int 	= 0;
//	VME.Lat_Second_frac 	= 0;
	if (SYSTEM.POSITION.CURRENT.LON > 0)		VME.Data.Lon_sign = 0;
	else if (SYSTEM.POSITION.CURRENT.LON < 0)	VME.Data.Lon_sign = 1;
	//GPGGA<<---initialize//
//	VME.Lon_Degree 		= 0;
//	VME.Lon_Minute 		= 0;
//	VME.Lon_Second_int 	= 0;
//	VME.Lon_Second_frac 	= 0;
	if (SYSTEM.POSITION.CURRENT.HEI > 0)		VME.Data.height_sign = 0;
	else if (SYSTEM.POSITION.CURRENT.HEI < 0)	VME.Data.height_sign = 1;
	VME.Data.height = (SYSTEM.POSITION.CURRENT.HEI) / 10;
//	VME.Data.Heading	 			= (align.heading.heading*100);

	VME.Data.Accuracy_Sec = (VME.Data.TOD_Valid << 15)	|| (VME.Data.GPS_Lock << 14);
	if (RAW_OFFSET < 0)			VME.Data.Accuracy_nSec_1 = ((((int) (fabs(RAW_OFFSET) * 100)) & 0x3FFF0000) >> 16) || 0x1000;
	else if (RAW_OFFSET < 0)	VME.Data.Accuracy_nSec_1 = ((((int) (fabs(RAW_OFFSET) * 100))	& 0x3FFF0000) >> 16);
	VME.Data.Accuracy_nSec_2 = ((int) (fabs(RAW_OFFSET) * 100)) & 0x0000FFFF;


//	VME.Data.Geodetic_Mode 	= 0;
//	VME.Data.M_S_Mode 		= CONFIG.iCVType;
//	VME.Data.TimeSync_Mode 	= 0;
	dbgCV(3360) {
		printf("\n[%d, %d, %s]\t\t\t\t\t\tVisible,Tracking,GPS_Lock\n",
				VME.Data.Visible, VME.Data.Tracking,
				(VME.Data.GPS_Lock == 1) ? "GPS Lock!" : "GPS UnLock!");
		printf("[%s, %s, %s, %s, %d]\tTOD,FPGA,TEMP,POWER(Valid)\n",
				(VME.Data.TOD_Valid == 1) ? "TOD_Valid" : "TOD_InValid",
				(VME.Data.FPGA_Valid == 1) ? "FPGA_Valid" : "FPGA_InValid",
				(VME.Data.Temp_Valid == 1) ? "Temp_Valid" : "Temp_InValid",
				(VME.Data.Power_Valid == 1) ? "Power_Valid" : "Power_InValid",
				TOD.status);
		printf(
				"[%04d, %02d, %02d, %02d, %02d, %02d]\t\t\tyear,month,day,hour,minute,second\n",
				VME.Data.Year, VME.Data.Month, VME.Data.Day, VME.Data.Hour,
				VME.Data.Minute, VME.Data.Second);
		printf(
				"[%d, %d, %02d, %02d, %04d]\t\t\t\tLat_sign,Lat_Degree,Lat_Minute,Lat_Second_init,Lat_Second_frac\n",
				VME.Data.Lat_sign, VME.Data.Lat_Degree, VME.Data.Lat_Minute,
				VME.Data.Lat_Second_int, VME.Data.Lat_Second_frac);
		printf(
				"[%d, %d, %02d, %02d, %04d]\t\t\t\tLon_sign,Lon_Degree,Lon_Minute,Lon_Second_init,Lon_Second_frac\n",
				VME.Data.Lon_sign, VME.Data.Lon_Degree, VME.Data.Lon_Minute,
				VME.Data.Lon_Second_int, VME.Data.Lon_Second_frac);
		printf("[%d, %d, %.4f]\t\t\t\tHeight,Heading\n", VME.Data.height_sign,
				VME.Data.height, VME.Data.Heading);
		printf("[%d, %d, %d]\t\t\t\tAccuracy\n", VME.Data.Accuracy_Sec,
				VME.Data.Accuracy_nSec_1, VME.Data.Accuracy_nSec_2);
		printf("[%s, %s, %s]\tGeo_Mode, M_S_Mode, TimeSync_Mode\n",
				(VME.Data.Geodetic_Mode == 1) ?
						"Geodetic_Mode" : "Not Geodetic_Mode",
				(VME.Data.M_S_Mode == 0) ? "Master_Mode" : "Slave_Mode",
				(VME.Data.TimeSync_Mode == VME_CV_MODE) ?
						"CV_MODE" :
						((VME.Data.TimeSync_Mode == VME_PTP_MODE) ?
								"VME_PTP_MODE" : "VME_ExtClock_MODE"));
		printf("%lf\n", RAW_OFFSET);
		printf("[%d, %d]\n", SYSTEM.POSITION.CURRENT.LAT,
				SYSTEM.POSITION.CURRENT.LON);
//		printf("[%d, %d, %d]\n",HEALTH.EB,DIAG.EB,DIAG.EB_LOCK);
	}
}

//==========================================//
// VME BlockRam에 쓸 데이터 형식에 맞게 변환		//
//==========================================//
void VME_Update_BlockRam_Data() {
	if (CONFIG.VMEMODE == 0)	return;

//	VME.BlockRam.Boart_ID		= 0;
	VME.BlockRam.Bit 	= ((VME.Data.Visible & 0x0F) << 12)
							+ ((VME.Data.Tracking & 0x0F) << 8)
							+ ((VME.Data.TOD_Valid & 0x01) << 4)
							+ ((VME.Data.FPGA_Valid & 0x01) << 3)
							+ ((VME.Data.Temp_Valid & 0x01) << 2)
							+ ((VME.Data.Power_Valid & 0x01) << 1)
							+ (VME.Data.GPS_Lock & 0x01);
	VME.BlockRam.Year 	= (VME.Data.Year & 0x0FFF);
	VME.BlockRam.Month_Day 	= ((VME.Data.Month & 0x0F) << 8) + (VME.Data.Day & 0x1F);
	VME.BlockRam.Hour_min 	= ((VME.Data.Hour & 0x1F) << 8) + (VME.Data.Minute & 0x3F);
	VME.BlockRam.Second 	= (VME.Data.Second & 0x3F);
	VME.BlockRam.Lat_Deg_Min 	= ((VME.Data.Lat_Degree & 0x7F) << 8) + ((VME.Data.Lat_sign & 0x01) << 6) + (VME.Data.Lat_Minute & 0x3F);
	VME.BlockRam.Lat_Sec_Int 	= (VME.Data.Lat_Second_int & 0x3F);
	VME.BlockRam.Lat_Sec_Frac 	= (VME.Data.Lat_Second_frac & 0x3FFF);
	VME.BlockRam.Lon_Deg_min 	= ((VME.Data.Lon_Degree & 0x7F) << 8) + ((VME.Data.Lon_sign & 0x01) << 6) + (VME.Data.Lon_Minute & 0x3F);
	VME.BlockRam.Lon_Sec_Int 	= (VME.Data.Lon_Second_int & 0x3F);
	VME.BlockRam.Lon_Sec_Frac 	= (VME.Data.Lon_Second_frac & 0x3FFF);
	VME.BlockRam.height 		= ((VME.Data.height_sign & 0x01) << 15) + (VME.Data.height & 0x7FFF);
//	VME.BlockRam.Heading		= (int)(VME.Data.Heading)&0xFFFF;
//	VME.BlockRam.CV			= 0;
//	VME.BlockRam.PTP		= 0;
	VME.BlockRam.Accuracy_Sec 	= VME.Data.Accuracy_Sec;
	VME.BlockRam.Accuracy_nSec_1 	= VME.Data.Accuracy_nSec_1 & 0x3FFF;
	VME.BlockRam.Accuracy_nSec_2 	= VME.Data.Accuracy_nSec_2;
//	VME.BlockRam.VME_Sync_Mode	= 0;
}

//========================================//
// VME BlockRam에 데이터 쓰기		     //
//========================================//
void VME_Write_BlockRam() {
	if (CONFIG.VMEMODE == 0)		return;

//	VME_BlockRam_Write(BLOCKRAM_ADDR_BOARDID,VME.BlockRam.Boart_ID);
	VME_BlockRam_Write(BLOCKRAM_ADDR_BIT, VME.BlockRam.Bit);
	VME_BlockRam_Write(BLOCKRAM_ADDR_YEAR, VME.BlockRam.Year);
	VME_BlockRam_Write(BLOCKRAM_ADDR_MONTH_DAY, VME.BlockRam.Month_Day);
	VME_BlockRam_Write(BLOCKRAM_ADDR_HOUR_MIN, VME.BlockRam.Hour_min);
	VME_BlockRam_Write(BLOCKRAM_ADDR_SECOND, VME.BlockRam.Second);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LAT_Deg_Min, VME.BlockRam.Lat_Deg_Min);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LAT_Sec_int, VME.BlockRam.Lat_Sec_Int);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LAT_Sec_Frac, VME.BlockRam.Lat_Sec_Frac);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LON_Deg_Min, VME.BlockRam.Lon_Deg_min);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LON_Sec_int, VME.BlockRam.Lon_Sec_Int);
	VME_BlockRam_Write(BLOCKRAM_ADDR_LON_Sec_Frac, VME.BlockRam.Lon_Sec_Frac);
	VME_BlockRam_Write(BLOCKRAM_ADDR_HEIGHT, VME.BlockRam.height);
//	VME_BlockRam_Write(BLOCKRAM_ADDR_HEADING,VME.BlockRam.Heading);
//	VME_BlockRam_Write(BLOCKRAM_ADDR_CV,VME.BlockRam.CV);		//CV Data
//	VME_BlockRam_Write(BLOCKRAM_ADDR_PTP,VME.BlockRam.PTP);		//PTP Data
	VME_BlockRam_Write(BLOCKRAM_ADDR_Accuracy_Sec, VME.BlockRam.Accuracy_Sec);
	VME_BlockRam_Write(BLOCKRAM_ADDR_Accuracy_nSec_1, VME.BlockRam.Accuracy_nSec_1);
	VME_BlockRam_Write(BLOCKRAM_ADDR_Accuracy_nSec_2, VME.BlockRam.Accuracy_nSec_2);
//	VME_BlockRam_Write(BLOCKRAM_ADDR_SYNC_MODE,VME.BlockRam.VME_Sync_Mode);
}

int CV_MODE_count = 0;
int PTP_MODE_count = 0;
int ExtClock_MODE_count = 0;
int M_S_Change = 0;
//========================================//
// VME BlockRam 데이터 읽기				//
// Ex) 동기화 모드 등							//
//========================================//
void VME_Read_BlockRam() {
	int temp;

	if (CONFIG.VMEMODE == 0){
		if(VME.T1000ms)	VME.T1000ms = OFF;
		return;
	}

	VME.Data.Init_Request = (VME_BlockRam_Read(BLOCKRAM_ADDR_INIT) & 0x0F); //Read Only
//	VME.Data.Bit_Request = (VME_BlockRam_Read(BLOCKRAM_ADDR_BIT) & 0x01); //Read Only
	temp = (VME_BlockRam_Read(BLOCKRAM_ADDR_SYNC_MODE) & 0x0F);
	VME.Data.Geodetic_Mode = (temp & 0x08) >> 3;
	VME.Data.M_S_Mode = (temp & 0x04) >> 2;
	VME.Data.TimeSync_Mode = (temp & 0x03);

	if( (VME.Data.Init_Request&0x08) ){
		printf("reset_sub_call \n");
		reset_sub_call();
	}
	if( (VME.Data.Init_Request&0x04) ){
		printf("Initialization \n");
		Initialization();
	}
	if( (VME.Data.Init_Request&0x02) ){
		printf("Init_CONTROL \n");
		Init_CONTROL();
		Init_BlockRam();
	}
	if( (VME.Data.Init_Request&0x01) ){
		printf("InitializeKalmanControl \n");
		InitializeKalmanControl();
		Init_BlockRam();
	}

	if ((VME.Data.TimeSync_Mode == 3)) { // && (CONFIG.VMEMODE==1) ){
		if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG)	printf("invalid TimeSync_Mode Value\n");
	}

//	if (VME.Data.Init_Request == ON) {
//		VME_BlockRam_Write(BLOCKRAM_ADDR_INIT, 0);
//		VME_BlockRam_Write(BLOCKRAM_ADDR_BOARDID, VME.BlockRam.Boart_ID);
//	}

	if (CONFIG.iCVType != VME.Data.M_S_Mode)	M_S_Change = ON;
	CONFIG.iCVType = VME.Data.M_S_Mode;
	CONFIG.WriteEvent = ON;

	if ((VME.Data.TimeSync_Mode == VME_CV_MODE)) {
		if (CV_MODE_count == 0) {
			printf("\n[ %s Mode ]\n"
					"[ %s Mode Start! ]\n", IsTime_MODE(),IsM_S_MODE());

			Initialization();
			Ext1PPS_EN_Low();
			//CONFIG.SYSTEM_DELAY=-10;
			//CONFIG.WriteEvent = ON;
			M_S_Change = OFF;
		}

		CV_MODE_count =1;
		PTP_MODE_count =0;
		ExtClock_MODE_count =0;
	}
	else if( VME.Data.TimeSync_Mode == VME_PTP_MODE ) {
		if(PTP_MODE_count==0) {
			printf("\n[ %s Mode ]\n"
					"[ %s Mode Start! ]\n",IsTime_MODE(),IsM_S_MODE());

			Initialization();
			Ext1PPS_EN_Low();
			ForceEBRun();
			//CONFIG.SYSTEM_DELAY=5;
			//CONFIG.WriteEvent = ON;
			M_S_Change = OFF;
		}

		CV_MODE_count =0;
		PTP_MODE_count =1;
		ExtClock_MODE_count =0;
	}
	else if( VME.Data.TimeSync_Mode == VME_ExtClock_MODE ) {
		if(ExtClock_MODE_count==0) {
			printf("\n[ %s Mode ]\n"
					"[ %s Mode Start! ]\n",IsTime_MODE(),IsM_S_MODE());

			Initialization();
			Ext1PPS_EN_High();
			ForceEBRun();
			//CONFIG.SYSTEM_DELAY=5;
			//CONFIG.WriteEvent = ON;
			M_S_Change = OFF;
		}

		CV_MODE_count =0;
		PTP_MODE_count =0;
		ExtClock_MODE_count =1;
	}

	if (M_S_Change) {
		printf("\n[ %s Mode ]\n"
				"[ %s Mode Start! ]\n", IsTime_MODE(),IsM_S_MODE());

		Initialization();

		M_S_Change = OFF;
	}

		//CV Data Read//
	if ((VME.Data.TimeSync_Mode == VME_CV_MODE) && (CONFIG.iCVType == 1)&& (CONFIG.NETSEND == 0)) {
		VME_CV_read();
	}

	//PTP Data Read//
	if (VME.T1000ms) {
		if (VME.Data.TimeSync_Mode == VME_PTP_MODE) {
			VME.BlockRam.PTP_Data_Sec = VME_BlockRam_Read(BLOCKRAM_ADDR_PTP_Sec);
			VME.BlockRam.PTP_Data_nSec_1 = VME_BlockRam_Read(BLOCKRAM_ADDR_PTP_nSec_1);
			VME.BlockRam.PTP_Data_nSec_2 = VME_BlockRam_Read(BLOCKRAM_ADDR_PTP_nSec_2);
		}
		if (((VME.BlockRam.PTP_Data_nSec_1 & 0x8000) >> 15) == 1)
			VME.Data.PTP_Data = -(((VME.BlockRam.PTP_Data_nSec_1 & 0x3FFF) << 15) + VME.BlockRam.PTP_Data_nSec_2);
		else if (((VME.BlockRam.PTP_Data_nSec_1 & 0x8000) >> 15) != 1)
			VME.Data.PTP_Data = (((VME.BlockRam.PTP_Data_nSec_1 & 0x3FFF) << 15) + VME.BlockRam.PTP_Data_nSec_2);

		dbgCV(3362)
			printf("PTP_Data : %d\n", VME.Data.PTP_Data);
	}

	if (VME.T1000ms){
		dbgCV(3361)
					printf("[%d, %d, %s, %s, %s]\tinit_Result,Bit_Request,,,\n",
							VME.Data.Init_Result, VME.Data.Bit_Request,
							(VME.Data.Geodetic_Mode == 1) ?
									"Geodetic_Mode" : "Not Geodetic_Mode", IsM_S_MODE(),
							IsTime_MODE());

		VME.T1000ms = OFF;
	}

}

void VME_Write(int addr, int data) {
	/*		Write Timing	*/
	int counter;
	if (CONFIG.VMEMODE == 1) {
		dbgCV(3363)		printf("Not Master Mode!\n");
		return;
	}
	if(addr>=VME_CV_STOP_ADDR__){
		printf("VME_Write return\n");
		return;
	}

	addr+=VME_START_ADDR;

	VME_SYSRST_Low();
//	usleep(20);
	VME_SYSRST_High();
//	usleep(10);

	for (counter = 0; counter < 7; counter++) {
		switch (counter) {
		case 0: //init
			VME_ADDR(addr);
			VME_DATA(data);

			VME_IACK_Low();
			VME_AS_High();
			VME_WR_High();
			VME_DS1_High();
			VME_DS0_High();
			VME_DATA_EN_High();
			break;
		case 1:
//				VME_ADDR(addr);
			VME_AM();
			VME_LWORD_Low();
			VME_IACK_High();
			VME_IACKIN_Low();
			VME_WR_Low();
			break;
		case 2:
			VME_AS_Low();
			VME_DS1_Low();
			VME_DS0_Low();
			break;
		case 3:
//				VME_DATA(data);
			break;
		case 4:
			VME_AS_High();
			VME_WR_High();
			break;
		case 5:
			VME_DS1_High();
			VME_DS0_High();
			break;
		case 6:
			VME_IACKIN_High();

			VME_DATA_EN_Low();

			break;
		}
	}
}

int VME_Read(int addr) {
	//		Read Timing		//
	int counter;
	int data = 0;

	if (CONFIG.VMEMODE == 1) {
		dbgCV(3364)
			printf("Not Master Mode!\n");
		return 0;
	}

	addr+=VME_START_ADDR;

	VME_SYSRST_Low();
//	usleep(20);
	VME_SYSRST_High();
//	usleep(10);

	for (counter = 0; counter < 6; counter++) {
		switch (counter) {
		case 0: //init
			VME_ADDR(addr);

			VME_AS_High();
			VME_IACK_Low();
			VME_WR_Low();
			VME_DS1_High();
			VME_DS0_High();
			VME_M_Read_EN_High();
			break;
		case 1:
			VME_AM()
			;
			VME_LWORD_Low();
			VME_IACK_High();
			VME_IACKIN_Low();
			VME_WR_High();
			break;
		case 2:
			VME_AS_Low();
			VME_DS1_Low();
			VME_DS0_Low();
			break;
		case 3:
			//Data Read
			data = VME_M_ReadData();
			break;
		case 4:
			VME_AS_High();
			VME_WR_Low();
			VME_DS1_High();
			VME_DS0_High();
			break;
		case 5:
			VME_IACKIN_High();

			VME_M_Read_EN_Low();

			break;
		}
	}
	return data;
}

void VME_Interrupt() {
	//		Interrupt Timing		//
	int counter;
	int data = 0;

	if (CONFIG.VMEMODE == 1) {
		dbgCV(3364)	printf("Not Master Mode!\n");
		return;
	}

	VME_SYSRST_Low();
//	usleep(20);
	VME_SYSRST_High();
//	usleep(10);

	for (counter = 0; counter < 30; counter++) {
		switch (counter) {
		case 0: //init
			VME_ADDR(0x8);
			VME_AS_High();
			VME_IACK_High();
			VME_IACKIN_High();
			VME_DS0_High();
			VME_DS1_High();
			VME_LWORD_High();
			VME_WR_High();
			break;
		case 2:
			VME_IACK_Low();
			VME_DS0_Low();
			VME_DS1_High();
			VME_LWORD_High();
			VME_WR_High();
			VME_IACKIN_Low();
			break;
		case 7:
			VME_AS_Low();
			break;
		case 29:
			VME_IACK_High();
			VME_ADDR(0);
			VME_IACKIN_High();
			VME_AS_High();
			VME_DS0_High();
			VME_DS1_High();
			VME_LWORD_High();
			VME_WR_High();
			break;
		}
	}
}
//======================================//
//sewon 2012-06-28						//
//클럭튜닝완료 인터럽트 or CV Data 전송 요청	//
//======================================//
void VME_IRQ4_Interrupt() {
	/*		Write Timing	*/
	int counter;

	if (CONFIG.VMEMODE == 0) {
		dbgCV(3363)
			printf("Not Slave Mode!\n");
		return;
	}

	for (counter = 0; counter < 7; counter++) {
		switch (counter) {
		case 0:
			break;
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		}
	}
}

void VME_BlockRam_Write(int addr, int data) {
	if(CONFIG.VMEMODE == 0)		return;
	if(addr>=VME_CV_STOP_ADDR__){
		printf("Block return\n");
		return;
	}

	VME_BlockRam_ADDR(addr);
	VME_BlockRam_DATA(data);

	VME_BlockRam_wea_High();

	VME_BlockRam_wea_Low();
}

int VME_BlockRam_Read(int addr) {
	int data = 0;
	if (CONFIG.VMEMODE == 0)	return -1;


	VME_BlockRam_ADDR(addr);

	VME_BlockRam_wea_Low();

	VME_IOBUF_EN_High();
	data = VME_ReadData();
	VME_IOBUF_EN_Low();

	return data;
}

//======================================================//
//		* CV Master에서 CV Data 쓰기						//
//			1. CV Master BlockRam에 적기					//
//			2. VME Master 에서 Slave BlockRam에 적기		//
//======================================================//
void PushCVData(char* p, unsigned int length)
{
	_CV_Data *CV = &VME.CV_Data;

	dbgCV(3772){
		printf("PushCVData[%d]\n",CV->Tick);
		printf("pp>%s",p);
	}

	//==================================//
	//버퍼 다 안 채웠을 때에 기존버퍼에 추가저장	//
	//==================================//
	if(CV->Buf[CV->Tick].full!=ON){
		CV->Buf[CV->Tick].size += length;
		dbgCV(3778){
			printf("[%d][%d][%d][%d]\n", CV->Tick, length, CV->Buf[CV->Tick].size, CV->Buf[!(CV->Tick)].size);
		}
		//CV Data 쌓기
		strcat(CV->Buf[CV->Tick].b , p);
	}
	if( (CV->Rang_Count>=CV->CV_Trans_Period) && (CV->Satxyz_Count>=CV->CV_Trans_Period) ){
		CV->Rang_Count 	= 0;
		CV->Satxyz_Count	= 0;
		CV->Buf[CV->Tick].full = ON;
		CV->Tick ^= ON;
	}
}

//======================================================//
//		* CV Master에서 CV Data 쓰기						//
//			1. CV Master BlockRam에 적기					//
//			2. VME Master에서 Slave BlockRam에 적기		//
//======================================================//
void PopCVData()
{
	int i;
	int temp;
	int CV_START_ADDR;
	int size;
	_CV_Data *CV = &VME.CV_Data;
	if ((VME.Data.TimeSync_Mode != VME_CV_MODE) || (CONFIG.iCVType == 1)) {
		return;
	}

	if(CONFIG.VMEMODE == 0)	CV_START_ADDR = VME_CV_START_ADDR__;
	else			CV_START_ADDR = BLOCKRAM_CV_START_ADDR__;


	//====================================//
	//버퍼 다 채웠을 때에 블럭램 적기 및 변수 초기화//
	//====================================//
	if(CV->Buf[!(CV->Tick)].full==ON){
		//CV Data Length Check!//
//		if(CONFIG.VMEMODE == 0)	size = VME_Read(VME_ADDR_CV);
//		else					size = VME_BlockRam_Read(BLOCKRAM_ADDR_CV);
//		if(size!=0){
//			CV->Buf[!(CV->Tick)].size = 0;
//			CV->Buf[!(CV->Tick)].full = OFF;
//			memset(CV->Buf[!(CV->Tick)].b,0,M_CV_BUF_LEN);
//			if( SYSTEM.IO.CTRL_PORT.MODE == UDEBUG )	printf("###[CV Data Length Not Zero!]###\n");
//			return;
//		}

		if( SYSTEM.IO.CTRL_PORT.MODE == UDEBUG )	printf("###[CV Data Write_Start![%d][%d][%d]]###\n", !(CV->Tick), CV->Buf[!(CV->Tick)].size, CV->Buf[CV->Tick].size);

		//BlockRam 에 적기 VME Master는 VME Slave에 적기//
		if(CONFIG.VMEMODE == 0){
			for (i = 0; i < CV->Buf[!(CV->Tick)].size; i += 2, CV_START_ADDR += 2) {
				temp = ((CV->Buf[!(CV->Tick)].b[i] & 0xFF) << 8) + (CV->Buf[!(CV->Tick)].b[i + 1] & 0xFF);
				VME_Write(CV_START_ADDR, temp);
			}
			VME_Write(VME_ADDR_CV, CV->Buf[!(CV->Tick)].size);
		}
		else{
			for (i = 0; i < CV->Buf[!(CV->Tick)].size; i += 2, CV_START_ADDR ++) {
				temp = ((CV->Buf[!(CV->Tick)].b[i] & 0xFF) << 8) + (CV->Buf[!(CV->Tick)].b[i + 1] & 0xFF);
				VME_BlockRam_Write(CV_START_ADDR, temp);
			}
			VME_BlockRam_Write(BLOCKRAM_ADDR_CV, CV->Buf[!(CV->Tick)].size);

			// Send Req Interrupt //
			// ... //
			VME_IRQ4_REQ_High();
		}

		//  초기화 //
		CV->Buf[!(CV->Tick)].size = 0;
		CV->Buf[!(CV->Tick)].full	= OFF;
		memset(CV->Buf[!(CV->Tick)].b,0,M_CV_BUF_LEN);
	}
}

//VME.CV_Data.Tick = ON;	현재 데이터 저장중인 배열
//VME.CV_Data.Tick = OFF; 	저장 완료 후 전송 할 배열
int VME_CV_write(char * p, unsigned int length) {

	int i = 0;
	int temp = 0;

	if ((CONFIG.NETSEND == 1))	return NO;
	if ((CONFIG.VMEMODE == 0) && ((VME_Read(VME_ADDR_SYNC_MODE) & 0x7) != 0x4)){
		dbgCV(3770)		printf("Not VME Slave is CV Mode!\n");
		return NO;
	}
	if ((VME.Data.TimeSync_Mode != VME_CV_MODE) || (CONFIG.iCVType == 1)) {
		dbgCV(3770)		printf("Not Common View Master Mode!\n");
		return NO;
	}
	if ((p[1] == 'T') && (p[2] == 'I') && (p[3] == 'M')) return NO;
	if (VME.CV_Data.CV_Data_Disable == ON)	return NO;


	if (SYSTEM.EB.LOCKMODE == EB_LOCKED){
		if ((p[1] == 'R') && (p[2] == 'A') && (p[3] == 'N')){
			VME.CV_Data.Rang_Count++;
			dbgCV(3770)		printf("Rang_Count : %d\n",VME.CV_Data.Rang_Count);
		}
		if ((p[1] == 'S') && (p[2] == 'A') && (p[3] == 'T')){
			VME.CV_Data.Satxyz_Count++;
			dbgCV(3770)		printf("Satxyz_Count : %d\n",VME.CV_Data.Satxyz_Count);
		}

		PushCVData(p,length);

	}
	return OK;
}

int CV_Data_Parse_string(char *cmdline, char *delim, char **argv)
{
	char *tok;
	int argc = 0;
	argv[argc] = NULL;

	for( (tok = strtok(cmdline, delim)) ; (tok && (argc<400)) ; (tok = strtok(NULL, delim)) )
	{
		argv[argc++] = tok;
	}
	return argc;
}
//=======================================================//
//		* CV Slave에서 CV Data 읽기						//
//			1. CV Slave BlockRam에 읽기					//
//			2.  다 읽었으면 Length 초기화						//
//=======================================================//
int VME_CV_read() {
	int i = 0;
	int num = 0;
	int temp = 0;
	char *CV_Tok[100];
	int CV_START_ADDR = BLOCKRAM_CV_START_ADDR__;
	int size;
	_CV_Data *CV = &VME.CV_Data;

	if ((CONFIG.VMEMODE == 0) || (CONFIG.NETSEND == 1))						return NO;
	if ((VME.Data.TimeSync_Mode != VME_CV_MODE) || CONFIG.iCVType == 0) 	return NO;

	//===CV Slave -> CV data Read===//
	size = VME_BlockRam_Read(BLOCKRAM_ADDR_CV);

	if( size != 0 ){

		memset(CV->Buf[CV->Tick].b, 0, M_CV_BUF_LEN);

		//===CV Slave -> CV data Read===//
		for (i = 0; i < size; i += 2, CV_START_ADDR ++) {
			temp = VME_BlockRam_Read(CV_START_ADDR);
			CV->Buf[CV->Tick].b[i] 		= ((temp>>8)&0xFF);
			CV->Buf[CV->Tick].b[i+1] 	= (temp&0xFF);
		}

		//===CV Slave -> CV data Tok===//
		num = CV_Data_Parse_string(CV->Buf[CV->Tick].b,"\n",CV_Tok);

		for(i=0;i<num;i++){

			oemv3_msg_rcvd( CV_Tok[i], 0, strlen(CV_Tok[i]), MDATA, fdEB);

			//#RCUPOS continue;
			if(strlen(CV_Tok[i])<100)	continue;

			//==========COMPARE==========//
			if (SYSTEM.EB.LOCKMODE == EB_LOCKED)	Compare(); //sewon 120622
			//===========================//

		}

		if((CV->CV_Value_Sum_Count==0)){
			VME_BlockRam_Write(BLOCKRAM_ADDR_CV,0);
			CV->CV_Value_Sum 		= 0;
			CV->CV_Value_Sum_Count 	= 0;
			return NO;
		}

		CONTROL.CV_Control_Counter = 0;
		dbRlt_global = (CV->CV_Value_Sum/CV->CV_Value_Sum_Count);
		if( SYSTEM.IO.CTRL_PORT.MODE == UDEBUG )	printf("###>>>compare avg : %lf \n",dbRlt_global);	//sewon 12-06-22

		//############[]############//
		printf("CONTROL.IntervalCounter:%d\n",CONTROL.IntervalCounter);
		if((SYSTEM.OPERATION.OPMODE==OPMODE_OPERATION)&&
				(((KInterval==Kinterval_STEP_1)&&(CONTROL.IntervalCounter!=0))||
				((KInterval==Kinterval_STEP_2)&&(CONTROL.IntervalCounter<5))||
				((KInterval==Kinterval_STEP_3)&&(CONTROL.IntervalCounter<5))||
				((KInterval==Kinterval_STEP_4)&&(CONTROL.IntervalCounter<5)))){
			printf("[%02d] [%02d]\n",CONTROL.IntervalCounter,CONTROL.CV_Control_Counter);
			CONTROL.IntervalCounter=0;
			TimeOffsetTable.InsertCount=0;
		}

		//===CV Slave Length 초기화===//
		VME_BlockRam_Write(BLOCKRAM_ADDR_CV,0);
		CV->CV_Value_Sum 		= 0;
		CV->CV_Value_Sum_Count 	= 0;
	}
	return OK;
}

void VME_Interrupt_handling()
{
	int temp[20];
	int pre_temp[20];
	EB_	*EB = &SYSTEM.EB;

	if(VME_IRQ4_IACK_IsBitHigh()){
		dbgCV(1212)printf("dd[%02x]\n",GetFpgaValue(24));
		VME_IRQ4_REQ_Low();
	}

	if (CONFIG.VMEMODE == 0)	return;

	temp[0]		= VME_BlockRam_Read(BLOCKRAM_ADDR_BOARDID);
	temp[1]		= VME_BlockRam_Read(BLOCKRAM_ADDR_BIT);
	temp[2]		= VME_BlockRam_Read(BLOCKRAM_ADDR_YEAR);
	temp[3]		= VME_BlockRam_Read(BLOCKRAM_ADDR_MONTH_DAY);
	temp[4]		= VME_BlockRam_Read(BLOCKRAM_ADDR_HOUR_MIN);
	temp[5]		= VME_BlockRam_Read(BLOCKRAM_ADDR_SECOND);
	temp[6]		= VME_BlockRam_Read(BLOCKRAM_ADDR_LAT_Deg_Min);
	temp[7]		= VME_BlockRam_Read(BLOCKRAM_ADDR_LAT_Sec_int);
	temp[8]		= VME_BlockRam_Read(BLOCKRAM_ADDR_LAT_Sec_Frac);
	temp[9]		= VME_BlockRam_Read(BLOCKRAM_ADDR_LON_Deg_Min);
	temp[10]	= VME_BlockRam_Read(BLOCKRAM_ADDR_LON_Sec_int);
	temp[11]	= VME_BlockRam_Read(BLOCKRAM_ADDR_LON_Sec_Frac);
	temp[12]	= VME_BlockRam_Read(BLOCKRAM_ADDR_HEIGHT);
	temp[13]	= VME_BlockRam_Read(BLOCKRAM_ADDR_PTP_Sec);
	temp[14]	= VME_BlockRam_Read(BLOCKRAM_ADDR_PTP_nSec_1);
	temp[15]	= VME_BlockRam_Read(BLOCKRAM_ADDR_PTP_nSec_2);
	temp[16]	= VME_BlockRam_Read(BLOCKRAM_ADDR_SYNC_MODE);

	if((VME.BlockRam.Boart_ID)!=(temp[0])){
		printf("temp[0]	[%02x][%02x]\n",VME.BlockRam.Boart_ID,temp[0]);
		if((temp[0]&&0x08)){
			printf("reset_sub_call();\n");
			reset_sub_call();
		}
		else if((temp[0]&&0x04)){
			printf("Initialization() \n");
			Initialization();
		}
		else if((temp[0]&&0x02)){
			printf("Init_CONTROL() \n");
			Init_CONTROL();
		}
		else if((temp[0]&&0x01)){
			printf("InitializeKalmanControl() \n");
			InitializeKalmanControl();
		}
	}
	if((VME.BlockRam.Bit)!=(temp[1])){
		printf("temp[1] [%02x][%02x]\n",VME.BlockRam.Bit,temp[1]);
		if(temp[1]==1){
			VME_BlockRam_Write(BLOCKRAM_ADDR_BIT, VME.BlockRam.Bit);
		}
	}
	if((VME.BlockRam.Year)!=(temp[2])){
		printf("temp[2] [%02x][%02x]\n",VME.BlockRam.Year,temp[2]);
		EB->TIME.YEAR = temp[2];
	}
	if((VME.BlockRam.Month_Day)!=(temp[3])){
		printf("temp[3] [%02x][%02x]\n",VME.BlockRam.Month_Day,temp[3]);
		EB->TIME.MONTH	= (temp[3]&&0x0F00);
		EB->TIME.DAY	= (temp[3]&&0x001F);
	}
	if((VME.BlockRam.Hour_min)!=(temp[4])){
		printf("temp[4] [%02x][%02x]\n",VME.BlockRam.Hour_min,temp[4]);
		EB->TIME.HOUR	= (temp[4]&&0x1F00);
		EB->TIME.MINUTE	= (temp[4]&&0x003F);
	}
	if((VME.BlockRam.Second)!=(temp[5])){
		printf("temp[5] [%02x][%02x]\n",VME.BlockRam.Second,temp[5]);
		EB->TIME.SECOND=SYSTEM.TIME.MID.SECOND=SYSTEM.TIME.GPS.SECOND	= (temp[5]&&0x003F);
	}
	if((VME.BlockRam.Lat_Deg_Min)!=(temp[6])){
		printf("temp[6] [%02x][%02x]\n",VME.BlockRam.Lat_Deg_Min,temp[6]);

	}
	if((VME.BlockRam.Lat_Sec_Int)!=(temp[7])){
		printf("temp[7] [%02x][%02x]\n",VME.BlockRam.Lat_Sec_Int,temp[7]);
	}
	if((VME.BlockRam.Lat_Sec_Frac)!=(temp[8])){
		printf("temp[8] [%02x][%02x]\n",VME.BlockRam.Lat_Sec_Frac,temp[8]);
	}
	if((VME.BlockRam.Lon_Deg_min)!=(temp[9])){
		printf("temp[9] [%02x][%02x]\n",VME.BlockRam.Lon_Deg_min,temp[9]);
	}
	if((VME.BlockRam.Lon_Sec_Int)!=(temp[10])){
		printf("temp[10] [%02x][%02x]\n",VME.BlockRam.Lon_Sec_Int,temp[10]);
	}
	if((VME.BlockRam.Lon_Sec_Frac)!=(temp[11])){
		printf("temp[11] [%02x][%02x]\n",VME.BlockRam.Lon_Sec_Frac,temp[11]);
	}
	if((VME.BlockRam.height)!=(temp[12])){
		printf("temp[12] [%02x][%02x]\n",VME.BlockRam.height,temp[12]);
	}
//	if((VME.BlockRam.PTP_Data_Sec)!=(temp[13])){
//		printf("temp[13] [%02x][%02x]\n",VME.BlockRam.PTP_Data_Sec,temp[13]);
//	}
//	if((VME.BlockRam.PTP_Data_nSec_1)!=(temp[14])){
//		printf("temp[14] [%02x][%02x]\n",VME.BlockRam.PTP_Data_nSec_1,temp[14]);
//	}
//	if((VME.BlockRam.PTP_Data_nSec_2)!=(temp[15])){
//		printf("temp[15] [%02x][%02x]\n",VME.BlockRam.PTP_Data_nSec_2,temp[15]);
//	}
//	if((VME.BlockRam.VME_Sync_Mode)!=(temp[16])){
//		printf("temp[16] [%02x][%03d]\n",temp[16],temp[16]);
//	}

}

//int Pre_Addr_0	= 0;
//int Pre_Addr_1	= 0;
//int Pre_Data_0	= 0;
//int Pre_Data_1	= 0;
//void VME_Interrupt_handling()
//{
//	int Addr_0	= 0;
//	int Addr_1	= 0;
//	int Data_0	= 0;
//	int Data_1	= 0;
//
//	int i=400;
//
//	if (CONFIG.VMEMODE == 0)	return;
//
//	if(VME_IRQ4_IACK_IsBitHigh()){
////		while(i--);
//		dbgCV(1212)printf("dd[%02x]\n",GetFpgaValue(24));
//		VME_IRQ4_REQ_Low();
//	}
//
//	if(VME_WRITE_ACK_IsBitHigh()){
//		Addr_0 	= GetFpgaValue(VME_INT_ADDR_0);
//		Addr_1 	= GetFpgaValue(VME_INT_ADDR_1);
//		Data_0 	= GetFpgaValue(VME_INT_DATA_0);
//		Data_1 	= GetFpgaValue(VME_INT_DATA_1);
//
//		dbgCV(1313)printf("WRITE_ACK(1)\n");
//
//		if(((Addr_0==0)&(Addr_1==0)&(Data_0==0)&(Data_1==0))||((Pre_Addr_0==Addr_0)&&(Pre_Data_0==Data_0)&&(Pre_Data_1==Data_1)))	return;
//
//		dbgCV(1313) printf("WRITE_ACK(2)\n");
//
//		//if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG)
//		if(!(Addr_1&&0xFF)) printf("[0x%02x] [0x%02x] [0x%02x] [0x%02x]\n",Addr_1,Addr_0,Data_1,Data_0);
//
////		VME_WRITE_REQ_High();
////		VME_WRITE_REQ_Low();
//
//		Pre_Addr_0	= Addr_0;
//		Pre_Addr_1	= Addr_1;
//		Pre_Data_0	= Data_0;
//		Pre_Data_1	= Data_1;
//	}
//}

//	* Base Station:
//		OEMV2 FIX POSITION 36.43082206 127.39434762 75.0209
//		OEMV2 COM COM3 115200 N 8 1 N OFF
//		OEMV2 INTERFACEMODE COM3 NONE RTCA OFF
//		OEMV2 LOG COM3 RTCAREF ONTIME 10
//		OEMV2 LOG COM3 RTCAOBS2 ONTIME 1
//		OEMV2 LOG COM3 RTCA1 ONTIME 5
//		OEMV2 SAVECONFIG
//
//	* Rover Station:
//		OEMV2 COM COM3 115200 N 8 1 N OFF
//		OEMV2 INTERFACEMODE COM3 RTCA NONE OFF
//		OEMV2 LOG COM1 BESTPOSA ONTIME 1
//		//OEMV2 LOG COM1 BESTXYZA ONTIME 1
//		OEMV2 SAVECONFIG
void Init_Geodetic_Mode_Func() //sewon 120626
{
	char buf[100];
	double Lat;
	double Lon;
	double Hei;

	//Master
	if (VME.Data.M_S_Mode == 0) {
		sprintf(buf, "FIX POSITION %.9lf %.9lf %.9lf\r\n", Lat, Lon, Hei);
		write(fdEB, buf, strlen(buf));
		sprintf(buf, "COM COM3 115200 N 8 1 N OFF\r\n");
		write(fdEB, buf, strlen(buf));
		sprintf(buf, "INTERFACEMODE COM3 NONE RTCA OFF\r\n");
		write(fdEB, buf, strlen(buf));
		sprintf(buf, "LOG COM3 RTCAREF ONTIME 10\r\n");
		write(fdEB, buf, strlen(buf));
		sprintf(buf, "LOG COM3 RTCAOBS2 ONTIME 1\r\n");
		write(fdEB, buf, strlen(buf));
		sprintf(buf, "LOG COM3 RTCA1 ONTIME 5\r\n");
		write(fdEB, buf, strlen(buf));
		sprintf(buf, "SAVECONFIG\r\n");
		write(fdEB, buf, strlen(buf));
	}
	//Rover
	else if (VME.Data.M_S_Mode == 1) {
		sprintf(buf, "COM COM3 115200 N 8 1 N OFF\r\n");
		write(fdEB, buf, strlen(buf));
		sprintf(buf, "INTERFACEMODE COM3 RTCA NONE OFF\r\n");
		write(fdEB, buf, strlen(buf));
		sprintf(buf, "LOG COM1 BESTPOSA ONTIME 1\r\n");
		write(fdEB, buf, strlen(buf));
//		sprintf(buf,"LOG COM1 BESTXYZA ONTIME 1\r\n");
//		write(fdEB,buf,strlen(buf));
		sprintf(buf, "SAVECONFIG\r\n");
		write(fdEB, buf, strlen(buf));
	}
}

